<?php


namespace App\Services;


use App\Models\Qtag;
use App\Utils\Code;
use App\Utils\Tree;

class QtagService extends BaseService
{

    /**
     * 创建数据
     */
    public function store(array $params) {
        if (!isset($params['name']) || !$params['name']) {
            throw new \Exception('名称不能为空', Code::PARAMETER_ERROR);
        }
        $model = new Qtag();
        if (isset($params['pid']) && is_numeric($params['pid'])){
            if ($params['pid'] && !$this->show($params['pid'], true)){
                throw new \Exception('不存在的上级ID主键', Code::PARAMETER_ERROR);
            }
            $model->pid = (int)$params['pid'];
            $model->full_name = Tree::instance()
                ->toParent($model->pid, $this->lists(['format'=>'lists']), 'id', 'name');
        }
        if (Qtag::where('name', $params['name'])->where('pid', (int)$model->pid)->first()) {
            throw new \Exception('该名称已经存在', Code::EXIST_REPEAT);
        }
        $model->name = $params['name'];
        if (isset($params['sort'])){
            $model->sort = (int)$params['sort'];
        }
        if (isset($params['status'])){
            $model->status = (int)$params['status'];
        }
        return $model->save();
    }

    /**
     * 数据信息
     * @param $id
     * @param bool $abnormal
     * @return Qtag|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
     * @throws \Exception
     */
    public function show($id, $abnormal = true) {
        $model = Qtag::where('id', $id)->first();
        if (!$model && $abnormal){
            throw new \Exception('不存在的ID主键', Code::EMPTY_DATA);
        }
        return $model;
    }

    /**
     * 更新数据
     */
    public function update($id, array $params) {
        $model = $this->show($id, true);
        if (isset($params['pid']) && is_numeric($params['pid'])) {
            if ($params['pid'] && !$this->show($params['pid'], false)) {
                throw new \Exception('不存在的上级ID主键', Code::PARAMETER_ERROR);
            }
            $model->pid = (int)$params['pid'];
        }
        if (isset($params['name']) && $params['name']) {
            if (Qtag::where('name', $params['name'])->where('pid', $model->pid)
                ->where('id', '<>', $model->id)->exists()) {
                throw new \Exception('名称重复', Code::EXIST_REPEAT);
            }
            $model->name = $params['name'];
        }
        $model->full_name = Tree::instance()
            ->toParent($model->pid, $this->lists(['format'=>'lists']), 'id', 'name');
        if (isset($params['sort']) && is_numeric($params['sort'])) {
            $model->sort = $params['sort'];
        }
        if (isset($params['status']) && is_numeric($params['status'])) {
            $model->status = $params['status'];
        }
        return $model->save();
    }

    /**
     * 删除数据
     * @param $id
     * @return bool|mixed|null
     * @throws \Exception
     */
    public function destroy($id) {
        $info = $this->show($id, true);
        if (!$this::$admin){
            throw new \Exception('你没有删除标签的权限', Code::LACK_OF_CONDITIONS);
        }
        return $info->delete();
    }

    /**
     * 标签列表
     * @param array $params
     * @param string $source
     * @return array
     */
    public function lists(array $params = [], $source = 'api') {
        $lists = Qtag::with([]);
        if ($source == 'manager') {
            if (isset($params['status']) && is_numeric($params['status'])) {
                $lists = $lists->where('status', (int)$params['status']);
            }
        } else {
            $lists = $lists->where('status', 1);
        }
        $lists = $lists->orderBy('sort', 'asc')->get()->toArray();
        if (isset($params['id']) && !empty($params['id'])) {
            $lists = Tree::instance()->toTreeChildren($lists, (int)$params['id'], 'id');
        }
        if (isset($params['format']) && $params['format'] == 'list'){
            $lists = Tree::instance()->treeFormat($lists, 'children', null);
        } else {
            $lists = Tree::instance()->toTree($lists, 'id', 'pid', 'children', false);
        }
        return $lists;
    }
}
